<?php

namespace app\service\admin;

use app\model\Common;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * Class UserService
 * @package projects\services\admin
 * @author Du
 * @date 2021/3/31
 * @time 11:37
 */
class UserService
{
    /**
     * 用户表1
     * @var string
     */
    public static string $user1 = 'user_1';
    /**
     * 用户表2
     * @var string
     */
    public static string $user2 = 'user_2';
    /**
     * 用户表3
     * @var string
     */
    public static string $user3 = 'user_3';

    /**
     * 查询字段
     * @var string[]
     */
    public static array $filed = ['id', 'user_name', 'sex', 'mobile', 'status', 'create_time', 'update_time'];

    /**
     * 获取用户列表
     * @param array $request
     * @param int $type
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author Du
     * @date 2021/3/31 11:44
     */
    public static function getList(array $request, int $type = 0): array
    {
        $page = $request['page'] ?? 1;
        $size = $request['size'] ?? 10;
        $so = $request['size'] ?? '';
        $filter[] = ['identity', '=', $type];
        if ($so) {
            $filter[] = [function ($query) use ($so) {
                $query->where('id', $so)->whereOr('name', $so)->whereOr('phone', $so);
            }];
        }
        return Common::getList(UserTable, $filter, self::$filed, $page, $size);
    }

    /**
     * 添加用户
     * @param array $request
     * @param int $type
     * @return int|string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author Du
     * @date 2021/3/31 17:49
     */
    public static function add(array $request, int $type = 0)
    {
        $id = Common::getCount(UserTable);
        $table = '';
        $request['id'] = $id + 1;
        switch ($request['id'] % 3) {
            case 0:
                $table = self::$user3;
                break;
            case 1:
                $table = self::$user1;
                break;
            case 2:
                $table = self::$user2;
                break;
        }
        $request['identity'] = $type;
        $request['password'] = password_hash($request['password'], PASSWORD_BCRYPT);
        return Common::addData($table, $request);
    }

    /**
     * 修改数据
     * @param array $request
     * @return int
     * @throws DbException
     * @author Du
     * @date 2021/4/1 15:26
     */
    public static function update(array $request): int
    {
        if (isset($request['password']) && $request['password']) {
            $request['password'] = password_hash($request['password'], PASSWORD_BCRYPT);
        } else {
            unset($request['password']);
        }
        return Common::updateData(UserTable, ['id' => $request['id']], $request);
    }

    /**
     * 删除/批量删除数据
     * @param array $request
     * @return false|int|string
     * @throws DbException
     * @author Du
     * @date 2021/4/1 16:30
     */
    public static function delete(array $request = [])
    {
        if (!isset($request['data']) || !$request['data']) {
            return false;
        }
        $ids = [];
        foreach ($request['data'] as $v) {
            $ids[] = $v['id'];
        }
        return Common::deleteData(UserTable, ['id' => $ids]);
    }
}